package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.MaterialRequestDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.common.ThreadLocalUtils;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;


    @Override
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        //存入minio中的文件名
        String uuid = UUID.randomUUID().toString();
        //获取扩展名
        String originalFilename = multipartFile.getOriginalFilename();
        String url = fileStorageService.uploadImgFile("", uuid + "." + originalFilename, multipartFile.getInputStream());
        //将图片数据保存到保存数据库
        Integer userId = ThreadLocalUtils.getUserId();
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(userId);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUrl(url);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public PageResponseResult list(MaterialRequestDto dto) {
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //user_id必须是登录用户id
        queryWrapper.eq(WmMaterial::getUserId, ThreadLocalUtils.getUserId());
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        queryWrapper.eq(dto.getIsCollection() != null && dto.getIsCollection() == 1,WmMaterial::getIsCollection,dto.getIsCollection());
        page(page,queryWrapper);
        long total = page.getTotal();
        List<WmMaterial> records = page.getRecords();
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(),total);
        pageResponseResult.setData(records);
        return pageResponseResult;
    }

    @Override
    public ResponseResult colleat(Integer id) {
        LambdaUpdateWrapper<WmMaterial> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(WmMaterial::getId,id).set(WmMaterial::getIsCollection,1);
        update(lambdaUpdateWrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult cancelcCollect(Integer id) {
        LambdaUpdateWrapper<WmMaterial> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(WmMaterial::getId,id).set(WmMaterial::getIsCollection,0);
        update(lambdaUpdateWrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
        //1.判断是素材否关联文章
        LambdaQueryWrapper<WmNewsMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmNewsMaterial::getMaterialId,id);
        Integer integer = wmNewsMaterialMapper.selectCount(lambdaQueryWrapper);
        //数量大于0表示有文章正在使用
        if (integer > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.MATERIAL_ALREADY_USED);
        }
        //删除minio中的素材
        WmMaterial material = getById(id);
        String url = material.getUrl();
        fileStorageService.delete(url);
        //删除数据库中的素材
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
